package com.livrexpress.livrexpressandroid.activity;

 
import com.livrexpress.livrexpressandroid.localdomain.Delivery;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
 
public class RefuseDelivery extends Activity { 
	
	private String idDelivery;
	private Button button_UnScanable;
	public Delivery livraison;
	
	private LocationManager locationManager = null;
	private Location loc;
	private String providerFine = null;
    
    
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_ACTION_BAR);
        setContentView(R.layout.refuse_delivery);
        Bundle extra = getIntent().getExtras();
        idDelivery = extra.getString("idDelivery");
        button_UnScanable = (Button) findViewById(R.id.button_UnScanable);
        
		//livraison
		livraison = new Delivery();
		livraison = Delivery.getDelivery(idDelivery);
        
        button_UnScanable.setOnClickListener(new OnClickListener() {
        	@Override
        	public void onClick(View v) {
        		
        		 EditText editview = (EditText)findViewById(R.id.edit);
        		 String raison = editview.getText().toString();
        		 livraison.setUndelivered(raison);
        		 livraison.setState(Delivery.STATE_NOTDELIVERED);
        		 
        		 locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);  
        		 Criteria criteria = new Criteria();                    
        		 criteria.setAltitudeRequired(false);
        		 criteria.setBearingRequired(false);
        		 criteria.setCostAllowed(true);
        		 criteria.setSpeedRequired(false);
        		 criteria.setPowerRequirement(Criteria.POWER_LOW);                      
        		 criteria.setAccuracy(Criteria.ACCURACY_FINE);
        		 providerFine = locationManager.getBestProvider(criteria, true);
        		 loc = locationManager.getLastKnownLocation(providerFine);
        		 livraison.setLatitude(""+loc.getLatitude());
        		 livraison.setLongitude(""+loc.getLongitude());
        		 livraison.update();
        		 
        		 openDeliveryList(v);
        	}
        	
        	public void openDeliveryList(View v){
                setResult(RESULT_OK,null);
        		finish();
        	}
        });
        
        
        
    }
}
